
//
//  Controller19.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/12.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit


//PullRefresh
class Controller19: UIViewController,UITableViewDataSource {

    var dataArray: Array<String> = ["😂", "🤗", "😳", "😌", "😊"]
    var tableView = UITableView()
    var refresh = UIRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI();
    }
    
    func configUI(){
        tableView.frame = view.bounds
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.backgroundColor = .white
        tableView.separatorInset = .zero
        tableView.tableFooterView = UIView()
        
        refresh.tintColor = .red
        refresh.backgroundColor = .gray
        tableView.addSubview(refresh)
        refresh.addTarget(self, action: #selector(refreshAction), for: .valueChanged)
        refresh.isUserInteractionEnabled = false
        
        let headerView = Bundle.main.loadNibNamed("HeaderAnimationView", owner: self, options: nil)?.first as? UIView
        headerView?.frame = refresh.bounds;
        headerView!.tag = 999;
//        refresh.addSubview(headerView!);
        headerView?.layoutIfNeeded();
        headerView?.setNeedsLayout();
    }
    
    func refreshAction(){
        print("change value")
        let time = DispatchTime.now() + .seconds(5)
//        let animationView = refresh.viewWithTag(999) as! HeaderAnimationView
//        animationView.animationStart()
        DispatchQueue.main.asyncAfter(deadline: time) {
            self.refresh.endRefreshing()
        }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "aaa")
        if cell == nil {
            cell = UITableViewCell(style: .default, reuseIdentifier: "aaa")
            cell?.backgroundColor = .clear;
            cell!.textLabel?.textAlignment = .center
        }
        cell!.textLabel!.text = dataArray[indexPath.row]
        return cell!;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    


}
